/*
 * Copyright (c) 2023, 2023,yzc.cn All rights reserved.
 *
 */
package com.crmapi.controller;

import com.crmapi.pojo.Customer;
import com.crmapi.pojo.PageBean;
import com.crmapi.pojo.Receive;
import com.crmapi.pojo.Result;
import com.crmapi.service.CustomerService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sound.midi.MidiSystem;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>Project: CRMSystem - CustomerController</p>
 * <p>Powered by yzc On 2023-11-23 09:14:57</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
@RestController
@RequestMapping("/api_v1.0")
@Api(tags = "公海客户管理")
@Slf4j
public class CustomerController extends BaseController{
   @Autowired
    CustomerService customerService;

   //分页条件查询
   @PostMapping("/customer/list")
   public Result queryList(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           String name, Integer  source, Integer level, Integer followStatus,Integer status){
       log.info("分页条件参数{},{},{},{},{},{},{}",page,pageSize,name,source,level,followStatus,status);
       PageBean pageBean = customerService.queryList(page, pageSize, name, source, level, followStatus,status);
       return Result.success(pageBean);
   }

   //添加客户
    @PostMapping("/customer/save")
    public Result insertCustomer(@RequestBody Customer customer){
       int rs = customerService.insertCustomer(customer);
       if (rs !=1 )
           return Result.error("添加失败");
       return Result.success("添加成功");
    }

    //批量删除
    @DeleteMapping("/customer/del/{ids}")
    public Result delCustomer(@PathVariable List<String> ids){
       log.info("批量删除操作,ids:{}",ids);
       int rs = customerService.del(ids);
       if (rs==0)
           return Result.error("删除失败");
       return Result.success("删除成功");
    }

    //跟新客户
    @PutMapping("/customer/update")
    public  Result updateCustomer(@RequestBody Customer customer){
       log.info("更新的客户信息为:{}",customer);
       int rs = customerService.updateCustomer(customer);
       if (rs !=1)
           return Result.error("更新失败");
       return Result.success("更新成功");
    }

    //领取客户
    @PutMapping("/customer/receive")
    public Result receive(@RequestBody Receive receive){
       log.info("被领取的客户id为：{}，领取人用户编号：{}，姓名：{}",receive.getIds(),receive.getUserCode(),receive.getUser());
       int rs = customerService.receive(receive);
       if (rs > 1 )
           return Result.success("领取客户成功");
       return Result.error("领取客户失败");
    }

    //放回公海
    @PutMapping("/customer/radiance/{ids}")
    public  Result radiance( @PathVariable List<String> ids){
       log.info("将id：{}的客户放回公海",ids);
       int rs = customerService.radiance(ids);
       if ( rs < 1 )
           return Result.error("客户放回公海失败");
       return Result.success("客户放回公海成功");
    }

    //查询全部客户
    @GetMapping("/customer/all")
    public Result queryAll(){
       List<Customer> customerList = customerService.queryAll();
       return Result.success(customerList);
    }

    //根据id差联系人
    @GetMapping("/customer/contact")
    public  Result queryContact(String id){
        String contact = customerService.queryContact(id);
        return Result.success(contact);
    }
}
